﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;

namespace BOSS.Web.Extensions
{
    /// <summary>
    /// 公共的常见扩展
    /// </summary>
    public static class CommonExtensions
    {
        private static readonly Regex removeBreakReturnRegex = new Regex(@"<br\s*/?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);

        private static readonly Encoding gb2312 = Encoding.GetEncoding("GB2312");

        /// <summary>
        /// 将字符串裁减到指定长度（半角）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string TrimTo(this string value, int maxLength)
        {
            return TrimTo(value, maxLength, string.Empty);
        }

        /// <summary>
        /// 将字符串裁减到指定长度（半角）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <param name="postfix"></param>
        /// <returns></returns>
        public static string TrimTo(this string value, int maxLength, string postfix)
        {
            value = value.Trim();
            if (value.Length == 0)
            {
                return value;
            }

            int sourceLength = gb2312.GetByteCount(value);
            if (sourceLength <= maxLength)
            {
                return value;
            }

            int index = 0;
            int length = 0;
            maxLength -= gb2312.GetByteCount(postfix);
            char[] chars = value.ToCharArray();
            while (length < maxLength)
            {
                length += gb2312.GetByteCount(chars, index, 1);
                if (length < maxLength)
                {
                    index++;
                }
            }
            return value.Substring(0, index) + postfix;
        }

        /// <summary>
        /// 获取枚举值的描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            Type enumType = value.GetType();
            string stringValue = value.ToString();

            FieldInfo fieldInfo = enumType.GetField(stringValue, BindingFlags.Public | BindingFlags.Static);
            if (fieldInfo == null)
            {
                return stringValue;
            }

            object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attributes.Length == 0)
            {
                return stringValue;
            }
            else
            {
                DescriptionAttribute description = attributes[0] as DescriptionAttribute;
                return description.Description;
            }
        }
    }
}
